package com.myk.game.lib.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.blankj.utilcode.util.FileIOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AssetsUtil {
    /**
     * 读取Assets目录中的文件名
     */
    public static List<String> readAssets2List(Context context, String assetsPath) {
        List<String> list = new ArrayList<>();
        AssetManager assetManager = context.getAssets();
        try {
            String[] names = assetManager.list(assetsPath);
            list.addAll(Arrays.asList(names));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 拷贝assets目录中的文件到本地
     */
    public static void copyFileFromAssets(Context context, String src, String dest) {
        AssetManager assetManager = context.getAssets();
        try {
            InputStream is = assetManager.open(src);
            FileIOUtils.writeFileFromIS(new File(dest), is);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
